#include "util.h"
#include <QTime>
#include <qdatetime.h>

PendantInterfaceInformationFrame pendantInterfaceInformationFrame;
PendantInterfaceInformation pendantInterfaceInformation;
PendantInterfaceData pendantInterfaceData;

QString userLevel;
QString userPasswd[4];
QSettings *settings;

const char* sendPath;
const char* receivePath;
const char* docPath;
const char* programPath;

QString ipAddressString;
QString tcp_port;
QHostAddress ipAddress;

safe_thread_queue<QString> TCP_AlarmMsg_queue;
safe_thread_queue<QString> TCP_SendMsg_queue;
QString data_buffer_QS;

Techn_Param_ Pendant_Techn_Param;
Techn_Param_ptr Pendant_Techn_Param_ptr;
AlarmNumForType_ PendantAlarmNumForType;

void setDefulatValue() {
    userLevel = "1";
    for(auto &x: userPasswd){
        x = "123";
    }
    sendPath = "/home/a64/gantry-robot/gantry-robot-master/Flexible2ControllerData.xml";
    receivePath = "/home/a64/gantry-robot/gantry-robot-master/ControllerData2Flexible.xml";
    docPath = "/home/a64/gantry-robot/gantry-robot-master/PendantStoreDoc.xml";
    programPath = "/home/a64/gantry-robot/gantry-robot-master/programdoc.xml";

    ipAddressString = "127.0.0.1";
    tcp_port = "9999";
    ipAddress = ipAddressString;
}

void initiateAlarmStruct()
{
    PendantAlarmNumForType.AlarmNum_OperateError = 2;
    PendantAlarmNumForType.AlarmNum_Axis = 3;

    Pendant_Techn_Param_ptr->OperateErrorAlarm.isAlarmTrigged = new bool[PendantAlarmNumForType.AlarmNum_OperateError];
    Pendant_Techn_Param_ptr->AxisAlarm.isAlarmTrigged = new bool[PendantAlarmNumForType.AlarmNum_Axis];

    Pendant_Techn_Param_ptr->OperateErrorAlarm.AlarmContent = new QString[PendantAlarmNumForType.AlarmNum_OperateError];
    Pendant_Techn_Param_ptr->AxisAlarm.AlarmContent = new QString[PendantAlarmNumForType.AlarmNum_Axis];

    Pendant_Techn_Param_ptr->OperateErrorAlarm.AlarmContent[0] = "abc1";
    Pendant_Techn_Param_ptr->OperateErrorAlarm.AlarmContent[1] = "abc2";
    Pendant_Techn_Param_ptr->AxisAlarm.AlarmContent[0] = "abc11";
    Pendant_Techn_Param_ptr->AxisAlarm.AlarmContent[1] = "abc222222222222222222222dcrvbbbb bbbbbbbbbbbbbbbbbbbbbbdfffffffff ffffffffffffffffffabc22222222222222 2222222dcrvbbbbbbbbbbbbbbbbbb bbbbbbbbdfffffff ffffffffff ffffffffff";
    Pendant_Techn_Param_ptr->AxisAlarm.AlarmContent[2] = "abc33";

    for(int i=0; i<PendantAlarmNumForType.AlarmNum_OperateError; i++)
    {
        Pendant_Techn_Param_ptr->OperateErrorAlarm.isAlarmTrigged[i] = 0;
    }
    for(int i=0; i<PendantAlarmNumForType.AlarmNum_Axis; i++)
    {
        Pendant_Techn_Param_ptr->AxisAlarm.isAlarmTrigged[i] = 0;
    }
}

void Sleep(int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

void setSpeedValue(QLabel* speedMutableLabel)
{
    speedMutableLabel->setText(pendantInterfaceData.speedMutableLabel);
}

void setIoStatus(QLabel* ledIndex, int color, QLabel* ioName, const char *ioNameText)
{
    setLED(ledIndex,color,16);
    ioName->setText(ioNameText);
}

void setTableCellColor(QTableWidget* table, int row, int col, const QString & color_name)
{
    const char *ColorName = color_name.toStdString().c_str();

    QColor *color;
    if(!strcmp(ColorName, "灰色"))
    {
        color = new QColor(190,190,190);
    }
    else if(!strcmp(ColorName, "红色"))
    {
        color = new QColor(255,0,0);
    }
    else if(!strcmp(ColorName, "绿色"))
    {
        color = new QColor(0,255,0);
    }
    else if(!strcmp(ColorName, "黄色"))
    {
        color = new QColor(255,255,0);
    }
    QRgb rgbValue = color->rgb();
    QIcon circleIcon = createCircleIcon(rgbValue);
    QTableWidgetItem *item = new QTableWidgetItem();
    item->setIcon(circleIcon);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    table->setItem(row, col, item);
}

void setTableCellText(QTableWidget* table, int row, int col, const QString & cell_text)
{
    QTableWidgetItem *item = new QTableWidgetItem();
    item->setText(cell_text);
    item->setForeground(Qt::black);
    item->setTextAlignment(Qt::AlignCenter);
    table->setItem(row, col, item);
}

void setLED(QLabel* label, int color, int size)
{
    label->setText("");
    QString min_width = QString("min-width: %1px;").arg(size);
    QString min_height = QString("min-height: %1px;").arg(size);
    QString max_width = QString("max-width: %1px;").arg(size);
    QString max_height = QString("max-height: %1px;").arg(size);
    QString border_radius = QString("border-radius: %1px;").arg(size/2);
    QString border = QString("border:1px solid black;");
    QString background = "background-color:";
    switch (color){
    case 0:
        // 灰色
        background += "rgb(190,190,190)";
        break;
    case 1:
        // 红色
        background += "rgb(255,0,0)";
        break;
    case 2:
        // 绿色
        background += "rgb(0,255,0)";
        break;
    case 3:
        // 黄色
        background += "rgb(255,255,0)";
        break;
    default:
        break;
    }
    const QString SheetStyle = min_width + min_height + max_width +
                               max_height + border_radius + border + background;
    label->setStyleSheet(SheetStyle);
}

bool ispendantInterfaceDataChange()
{
    static PendantInterfaceData prePendantInterfaceData;

    if (pendantInterfaceData.actionMode != prePendantInterfaceData.actionMode ||
        pendantInterfaceData.codeFileName != prePendantInterfaceData.codeFileName ||
        pendantInterfaceData.pageName != prePendantInterfaceData.pageName)
    {
        prePendantInterfaceData = pendantInterfaceData;
        return true;
    }

    return false;
}


void setPagesTopStyle(QWidget* widget)
{
    const QColor backgroundColor(229, 232, 237);
    const int borderRadius = 10;

    QFrame* frame = createFrame(widget, 800, 59);
    frame->setStyleSheet(QString("background-color: rgb(85, 87, 83); border-radius: 3px; border: 2px solid #000000;").arg(backgroundColor.name(), QString::number(borderRadius)));
    frame->move(1, 1);

    pendantInterfaceInformationFrame.pageName = createFrame(widget, 200, 40, {30, 10});
    pendantInterfaceInformationFrame.codeFileName = createFrame(widget, 200, 40, {230, 10});
    pendantInterfaceInformationFrame.actionMode = createFrame(widget, 130, 40, {430, 10});
    pendantInterfaceInformationFrame.realTime = createFrame(widget, 200, 40, {560, 10});
}

void setPagesTopFrame(QWidget* widget)
{
    QFrame* frame = createFrame(widget, 800, 59);
    frame->setStyleSheet(QString("background-color: rgb(85, 87, 83); border-radius: 3px; border: 2px solid #000000;"));
    frame->move(1, 1);


    QHBoxLayout *frame_layout = new QHBoxLayout(frame);

    pendantInterfaceInformation.pageName = new QLabel(pendantInterfaceInformationFrame.pageName);
    pendantInterfaceInformation.pageName->setText(pendantInterfaceData.pageName);
    pendantInterfaceInformation.pageName->setAlignment(Qt::AlignCenter);
    pendantInterfaceInformation.pageName->setStyleSheet("border: 2px solid #000000;background-color: rgb(238, 238, 236);font-size: 20px; color: black;");
    frame_layout->addWidget(pendantInterfaceInformation.pageName, 1);

    pendantInterfaceInformation.codeFileName = new QLabel(pendantInterfaceInformationFrame.codeFileName);
    pendantInterfaceInformation.codeFileName->setText(pendantInterfaceData.codeFileName);
    pendantInterfaceInformation.codeFileName->setAlignment(Qt::AlignCenter);
    pendantInterfaceInformation.codeFileName->setStyleSheet("border: 2px solid #000000;background-color: rgb(238, 238, 236);font-size: 20px; color: black;");
    frame_layout->addWidget(pendantInterfaceInformation.codeFileName, 1);

    pendantInterfaceInformation.actionMode = new QLabel(pendantInterfaceInformationFrame.actionMode);
    pendantInterfaceInformation.actionMode->setText(pendantInterfaceData.actionMode);
    pendantInterfaceInformation.actionMode->setAlignment(Qt::AlignCenter);
    pendantInterfaceInformation.actionMode->setStyleSheet("border: 2px solid #000000;background-color: rgb(238, 238, 236);font-size: 20px; color: black;");
    frame_layout->addWidget(pendantInterfaceInformation.actionMode, 1);

    pendantInterfaceInformation.realTime = new QLabel(pendantInterfaceInformationFrame.realTime);
    pendantInterfaceInformation.realTime->setText(pendantInterfaceData.realTime);
    pendantInterfaceInformation.realTime->setAlignment(Qt::AlignCenter);
    pendantInterfaceInformation.realTime->setStyleSheet("border: 2px solid #000000;background-color: rgb(238, 238, 236);font-size: 20px; color: black;");
    frame_layout->addWidget(pendantInterfaceInformation.realTime, 2);
}

void setPagesTopdisplayData()
{
    QDateTime realTime = QDateTime::currentDateTime();
    QString realTime2Str = realTime.toString("yyyy-MM-dd hh:mm:ss");
    pendantInterfaceData.pageName = "菜单";
    pendantInterfaceData.codeFileName = "NOW.ONE";
    pendantInterfaceData.actionMode = "手动操作";
    pendantInterfaceData.realTime = realTime2Str;
    pendantInterfaceData.speedMutableLabel = "低速";
}

void setPagesTopdisplayQlabel(QWidget* widget)
{
    pendantInterfaceInformation.pageName = new QLabel( pendantInterfaceInformationFrame.pageName);
    pendantInterfaceInformation.pageName->setText(pendantInterfaceData.pageName);
    pendantInterfaceInformation.pageName->move(20,15);

    pendantInterfaceInformation.codeFileName = new QLabel(pendantInterfaceInformationFrame.codeFileName);
    pendantInterfaceInformation.codeFileName->setText(pendantInterfaceData.codeFileName);
    pendantInterfaceInformation.codeFileName->move(10,15);

    pendantInterfaceInformation.actionMode = new QLabel(pendantInterfaceInformationFrame.actionMode);
    pendantInterfaceInformation.actionMode->setText(pendantInterfaceData.actionMode);
    pendantInterfaceInformation.actionMode->move(30,15);
    pendantInterfaceInformation.actionMode->setStyleSheet("QLabel{background-color:yellow;}");

    pendantInterfaceInformation.realTime = new QLabel(pendantInterfaceInformationFrame.realTime);
    pendantInterfaceInformation.realTime->setText(pendantInterfaceData.realTime);
    pendantInterfaceInformation.realTime->move(10,15);
}

QFrame* createSeparator()
{
    QFrame* separator = new QFrame();
    separator->setFrameShape(QFrame::VLine);
    separator->setFrameShadow(QFrame::Sunken);
    separator->setLineWidth(1);
    separator->setMidLineWidth(1);
    //separator->setStyleSheet("background-color: gray;");
    return separator;
}

void updateTimeLabel(QLabel* label)
{
    QDateTime realTime = QDateTime::currentDateTime();
    QString realTime2Str = realTime.toString("yyyy-MM-dd hh:mm:ss");
    label->setText(realTime2Str);
}

QLabel* createLabel(const QString &text)
{
    QLabel *label = new QLabel(text);
    label->setAlignment(Qt::AlignCenter);
    label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    label->setFrameStyle(QFrame::Panel | QFrame::Raised);
    label->setFixedWidth(150);
    return label;
}

QFrame* createFrame(QWidget* widget,int w,int h,const QPoint point)
{
    QFrame* frame = new QFrame(widget);
    frame->setFrameShape(QFrame::StyledPanel);
    frame->setFrameShadow(QFrame::Raised);
    frame->setLineWidth(1);
    frame->setMidLineWidth(0);
    frame->setFixedSize(w,h);
    frame->move(point);
    return frame;
}

void create_xml(const char* xmlPath)
{
    std::ifstream file(xmlPath);
    bool fileExists = file.good();
    file.close();

    if (!fileExists) {
        XMLDocument doc;
        XMLDeclaration* declaration = doc.NewDeclaration();
        doc.InsertFirstChild(declaration);
        XMLElement* rootElement = doc.NewElement("root");
        doc.InsertEndChild(rootElement);
        if(xmlPath == sendPath){
            XMLElement* program = doc.NewElement("programElement");
            rootElement->InsertEndChild(program);
            XMLElement* programElement = doc.NewElement("programName");
            programElement->SetAttribute("ProgramName", "自动运转程序");
            program->InsertEndChild(programElement);
        }

        if (doc.SaveFile(xmlPath) == XML_SUCCESS) {
            std::cout << "XML file created successfully." << std::endl;
        } else {
            std::cout << "Error creating XML file." << std::endl;
        }
    }
}

void traversal_node(XMLNode* node) {
    if (!node) {
        return;
    }

    for (XMLNode* child = node->FirstChild(); child; child = child->NextSibling()) {
        traversal_node(child);
    }
}

void traversal_element(XMLElement* element, int depth) {
    if (!element) {
        return;
    }

    for (int i = 0; i < depth; ++i) {
    //    std::cout << "  ";
    }

   // std::cout << "Element name: " << element->Name() << std::endl;

    for (const XMLAttribute* attribute = element->FirstAttribute(); attribute; attribute = attribute->Next()) {
        for (int i = 0; i < depth; ++i) {
        //    std::cout << "  ";
        }
       // std::cout << "Attribute: " << attribute->Name() << " = " << attribute->Value() << std::endl;
    }

    for (XMLNode* child = element->FirstChild(); child; child = child->NextSibling()) {
        if (child->ToElement()) {
            traversal_element(child->ToElement(), depth + 1);
        }
    }
}

void parse_xml(const char* xmlPath) {
    XMLDocument doc;
    XMLError error = doc.LoadFile(xmlPath);
    if (error != XML_SUCCESS) {
        std::cerr << "Error loading XML file.222" << std::endl;
        return;
    }

    XMLElement* root = doc.RootElement();
    if (!root) {
        std::cerr << "No root element found in XML." << std::endl;
        return;
    }

    traversal_node(root);

    traversal_element(root, 0);
}

void saveButtonState(const QString& buttonName, bool pressed)
{
    tinyxml2::XMLDocument doc;

    if (doc.LoadFile(sendPath) != tinyxml2::XML_SUCCESS) {
        //qDebug() << "Error loading XML file.111";
        return;
    }

    tinyxml2::XMLElement* root = doc.RootElement();
    if (!root) {
        qDebug() << "No root element found in XML.";
        return;
    }

    tinyxml2::XMLElement* ioControllerElement = root->FirstChildElement(
                    "ioControllerElement");
    if (!ioControllerElement) {
        ioControllerElement = doc.NewElement("ioControllerElement");
        root->InsertEndChild(ioControllerElement);
    }

    tinyxml2::XMLElement* existingElement = ioControllerElement->FirstChildElement(
                    "ioControllerName");
    while (existingElement) {
        const char* existingName = existingElement->Attribute("Name");
        if (existingName && QString(existingName) == buttonName) {
            existingElement->SetAttribute("Pressed", pressed ? 1 : 0);
            doc.SaveFile(sendPath);
            return;
        }
        existingElement = existingElement->NextSiblingElement("ioControllerName");
    }

    tinyxml2::XMLElement* ioControllerName = doc.NewElement("ioControllerName");
    ioControllerName->SetAttribute("Name", buttonName.toStdString().c_str());
    ioControllerName->SetAttribute("Pressed", pressed ? 1 : 0);

    ioControllerElement->InsertEndChild(ioControllerName);
    doc.SaveFile(sendPath);
}

QIcon createCircleIcon(QColor color)
{
    int diameter = 50;
    QPixmap pixmap(diameter, diameter);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(color);
    painter.drawEllipse(0, 0, diameter, diameter);
    return QIcon(pixmap);
}


QString getStringFromTechnParam(double AxisPos, int EncodePos)
{
    QString str1 =  QString::number(AxisPos);
    QString str2 = " / ";
    QString str3 =  QString::number(EncodePos);
    QString result = str1 + str2 + str3;
    return result;
}

//完成TCP已接收/待发送数据 --> 全局结构体（结构体同步至显示）
void setIoParam(const XMLElement *elementptr, const XMLAttribute *ele_attribute_ptr, Techn_Param_ptr tech_param)
{
    // 解析 输入的 IO数据的 <X04  IoState="true"/> 格式
    // 解析 输出的 IO数据的 <EmbraceOpen1  IoState="true"/> 格式

    const char *ele_name = elementptr->Name();
    const char *ele_attribute_name = ele_attribute_ptr->Name();
    const char *ele_attribute_value = ele_attribute_ptr->Value();
    //qDebug() << "这里！！元素1 2 3分别是" << elementptr->Name() << ele_attribute_ptr->Name() << ele_attribute_ptr->Value();

    if(!strcmp(ele_attribute_value, "true"))
    {
        if(!strcmp(ele_name, "X04"))
        {
            tech_param->PendantIoParam.X04 = true;
        }
        if(!strcmp(ele_name, "Y01"))
        {
            tech_param->PendantIoParam.Y01 = true;
        }
        if(!strcmp(ele_name, "EmbraceOpen1"))
        {
            tech_param->PendantIoParam.EmbraceOpen1 = true;
        }
    }
    else
    {
        if(!strcmp(ele_name, "X04"))
        {
            tech_param->PendantIoParam.X04 = false;
        }
        if(!strcmp(ele_name, "Y01"))
        {
            tech_param->PendantIoParam.Y01 = false;
        }
        if(!strcmp(ele_name, "EmbraceOpen1"))
        {
            tech_param->PendantIoParam.EmbraceOpen1 = false;
        }
    }
}

//完成TCP已接收/待发送数据 --> 全局结构体（结构体同步至显示）
void setAlarmParam(const XMLElement *elementptr, const XMLAttribute *ele_attribute_ptr, Techn_Param_ptr tech_param)
{
   // 解析 输入的 Alarm数据的 <OperateErrorAlarm  AlarmCode="0"/> 格式
   // 解析 输出的 IO数据的 <AxisAlarm  AlarmCode="1"/> 格式

   const char *ele_name = elementptr->Name();
   const char *ele_attribute_name = ele_attribute_ptr->Name();
   const char *ele_attribute_value = ele_attribute_ptr->Value();
   //qDebug() << "这里！！元素1 2 3分别是" << elementptr->Name() << ele_attribute_ptr->Name() << ele_attribute_ptr->Value();

   int AlarmNum = std::atoi(ele_attribute_value);

   if(!strcmp(ele_name, "OperateErrorAlarm"))
   {
       Pendant_Techn_Param_ptr->OperateErrorAlarm.isAlarmTrigged[AlarmNum] = true;
   }
   else if(!strcmp(ele_name, "AxisAlarm"))
   {
       Pendant_Techn_Param_ptr->AxisAlarm.isAlarmTrigged[AlarmNum] = true;
   }
}

//完成全局结构体（结构体同步至显示） --> doc存储文档
void RestoreTechnParam2Doc(Techn_Param_ptr Pendant_Techn_Param_ptr, const char* xmlPath)
{
    XMLDocument XMLDoc_local;
    if(XMLDoc_local.LoadFile(xmlPath) != XML_SUCCESS)
    {
        // 构建XML数据结构
        XMLDeclaration* declaration = XMLDoc_local.NewDeclaration();
        XMLDoc_local.InsertFirstChild(declaration);
        XMLElement* Root = XMLDoc_local.NewElement("root");
        XMLDoc_local.InsertEndChild(Root);

        // AxisParam_节点
        XMLElement* AxisParam_ = XMLDoc_local.NewElement("AxisParam_");
        Root->InsertEndChild(AxisParam_);
        XMLElement* AxisY = XMLDoc_local.NewElement("AxisY");
        AxisParam_->InsertEndChild(AxisY);
        XMLElement* AxisMX = XMLDoc_local.NewElement("AxisMX");
        AxisParam_->InsertEndChild(AxisMX);
        XMLElement* AxisMZ = XMLDoc_local.NewElement("AxisMZ");
        AxisParam_->InsertEndChild(AxisMZ);
        XMLElement* AxisSX = XMLDoc_local.NewElement("AxisSX");
        AxisParam_->InsertEndChild(AxisSX);
        XMLElement* AxisSZ = XMLDoc_local.NewElement("AxisSZ");
        AxisParam_->InsertEndChild(AxisSZ);
        // IoParam_节点
        XMLElement* IoParam_ = XMLDoc_local.NewElement("IoParam_");
        Root->InsertEndChild(IoParam_);
        XMLElement* X04 = XMLDoc_local.NewElement("X04");
        IoParam_->InsertEndChild(X04);
        XMLElement* Y01 = XMLDoc_local.NewElement("Y01");
        IoParam_->InsertEndChild(Y01);
        XMLElement* EmbraceOpen1 = XMLDoc_local.NewElement("EmbraceOpen1");
        IoParam_->InsertEndChild(EmbraceOpen1);
        // 教导程序
        XMLElement* ProgramElement = XMLDoc_local.NewElement("ProgramElement");
        Root->InsertEndChild(ProgramElement);
        XMLElement* ProgramName = XMLDoc_local.NewElement("ProgramName");
        ProgramName->SetAttribute("ProgramName", "自动运转程序");
        ProgramElement->InsertEndChild(ProgramName);
        // ServoParam_节点
        XMLElement* ServoParam_ = XMLDoc_local.NewElement("ServoParam_");
        Root->InsertEndChild(ServoParam_);
        XMLElement* SpeedRatio = XMLDoc_local.NewElement("SpeedRatio");
        ServoParam_->InsertEndChild(SpeedRatio);
        XMLElement* CorrectiveAxisAcc = XMLDoc_local.NewElement("CorrectiveAxisAcc");
        ServoParam_->InsertEndChild(CorrectiveAxisAcc);
    }
    if (XMLDoc_local.SaveFile(xmlPath) == tinyxml2::XML_SUCCESS)
    {
        //qDebug() << "Doc修改成功!";
    }
    XMLDocument XMLDoc_local1;
    if(XMLDoc_local1.LoadFile(xmlPath) == tinyxml2::XML_SUCCESS)
    {
        XMLElement* Root = XMLDoc_local1.RootElement();
        if(Root)
        {
            // AxisParam_节点
            XMLElement* AxisParam_ = Root->FirstChildElement("AxisParam_");
            if(AxisParam_)
            {
                XMLElement* AxisY = AxisParam_->FirstChildElement("AxisY");
                if(AxisY)
                {
                    AxisY->SetAttribute("AxisY_Pos", Pendant_Techn_Param_ptr->AxisY.AxisPos);
                    AxisY->SetAttribute("AxisY_EncodePos", Pendant_Techn_Param_ptr->AxisY.EncodePos);
                    AxisY->SetAttribute("AxisY_IsZero", Pendant_Techn_Param_ptr->AxisY.IsZero);
                    AxisY->SetAttribute("AxisY_AxisAcc", Pendant_Techn_Param_ptr->AxisY.AxisAcc);
                    AxisY->SetAttribute("AxisY_AxisAccTime", Pendant_Techn_Param_ptr->AxisY.AxisAccTime);
                }
                XMLElement* AxisMX = AxisParam_->FirstChildElement("AxisMX");
                if(AxisMX)
                {
                    AxisMX->SetAttribute("AxisMX_Pos", Pendant_Techn_Param_ptr->AxisMX.AxisPos);
                    AxisMX->SetAttribute("AxisMX_EncodePos", Pendant_Techn_Param_ptr->AxisMX.EncodePos);
                    AxisMX->SetAttribute("AxisMX_IsZero", Pendant_Techn_Param_ptr->AxisMX.IsZero);
                    AxisMX->SetAttribute("AxisMX_AxisAcc", Pendant_Techn_Param_ptr->AxisMX.AxisAcc);
                    AxisMX->SetAttribute("AxisMX_AxisAccTime", Pendant_Techn_Param_ptr->AxisMX.AxisAccTime);
                }
                XMLElement* AxisMZ = AxisParam_->FirstChildElement("AxisMZ");
                if(AxisMZ)
                {
                    AxisMZ->SetAttribute("AxisMZ_Pos", Pendant_Techn_Param_ptr->AxisMZ.AxisPos);
                    AxisMZ->SetAttribute("AxisMZ_EncodePos", Pendant_Techn_Param_ptr->AxisMZ.EncodePos);
                    AxisMZ->SetAttribute("AxisMZ_IsZero", Pendant_Techn_Param_ptr->AxisMZ.IsZero);
                    AxisMZ->SetAttribute("AxisMZ_AxisAcc", Pendant_Techn_Param_ptr->AxisMZ.AxisAcc);
                    AxisMZ->SetAttribute("AxisMZ_AxisAccTime", Pendant_Techn_Param_ptr->AxisMZ.AxisAccTime);
                }
                XMLElement* AxisSX = AxisParam_->FirstChildElement("AxisSX");
                if(AxisSX)
                {
                    AxisSX->SetAttribute("AxisSX_Pos", Pendant_Techn_Param_ptr->AxisSX.AxisPos);
                    AxisSX->SetAttribute("AxisSX_EncodePos", Pendant_Techn_Param_ptr->AxisSX.EncodePos);
                    AxisSX->SetAttribute("AxisSX_IsZero", Pendant_Techn_Param_ptr->AxisSX.IsZero);
                    AxisSX->SetAttribute("AxisSX_AxisAcc", Pendant_Techn_Param_ptr->AxisSX.AxisAcc);
                    AxisSX->SetAttribute("AxisSX_AxisAccTime", Pendant_Techn_Param_ptr->AxisSX.AxisAccTime);
                }
                XMLElement* AxisSZ = AxisParam_->FirstChildElement("AxisSZ");
                if(AxisSZ)
                {
                    AxisSZ->SetAttribute("AxisSZ_Pos", Pendant_Techn_Param_ptr->AxisSZ.AxisPos);
                    AxisSZ->SetAttribute("AxisSZ_EncodePos", Pendant_Techn_Param_ptr->AxisSZ.EncodePos);
                    AxisSZ->SetAttribute("AxisSZ_IsZero", Pendant_Techn_Param_ptr->AxisSZ.IsZero);
                    AxisSZ->SetAttribute("AxisSZ_AxisAcc", Pendant_Techn_Param_ptr->AxisSZ.AxisAcc);
                    AxisSZ->SetAttribute("AxisSZ_AxisAccTime", Pendant_Techn_Param_ptr->AxisSZ.AxisAccTime);
                }
            }
            // IoParam_节点
            XMLElement* IoParam_ = Root->FirstChildElement("IoParam_");
            if(IoParam_)
            {
                XMLElement* X04 = IoParam_->FirstChildElement("X04");
                if(X04)
                    X04->SetAttribute("IoState", Pendant_Techn_Param_ptr->PendantIoParam.X04);
                XMLElement* Y01 = IoParam_->FirstChildElement("Y01");
                if(Y01)
                    Y01->SetAttribute("IoState", Pendant_Techn_Param_ptr->PendantIoParam.Y01);
                XMLElement* EmbraceOpen1 = IoParam_->FirstChildElement("EmbraceOpen1");
                if(EmbraceOpen1)
                    EmbraceOpen1->SetAttribute("IoState", Pendant_Techn_Param_ptr->PendantIoParam.EmbraceOpen1);
            }
            // ServoParam_节点
            XMLElement* ServoParam_ = Root->FirstChildElement("ServoParam_");
            if(ServoParam_)
            {
                XMLElement* SpeedRatio = ServoParam_->FirstChildElement("SpeedRatio");
                if(SpeedRatio)
                    SpeedRatio->SetAttribute("ServoValue", Pendant_Techn_Param_ptr->PendantServoParam.SpeedRatio);
                XMLElement* CorrectiveAxisAcc = ServoParam_->FirstChildElement("CorrectiveAxisAcc");
                if(CorrectiveAxisAcc)
                    CorrectiveAxisAcc->SetAttribute("ServoValue", Pendant_Techn_Param_ptr->PendantServoParam.CorrectiveAxisAcc);
            }
        }
    }
    if (XMLDoc_local1.SaveFile(xmlPath) == tinyxml2::XML_SUCCESS)
    {
        //qDebug() << "Doc修改成功!";
    }
}

//完成doc存储文档 --> 全局结构体（结构体同步至显示）
void RestoreDoc2TechnParam(Techn_Param_ptr Pendant_Techn_Param_ptr, const char* xmlPath)
{
    XMLDocument XMLDoc_local;
    if(XMLDoc_local.LoadFile(xmlPath) == tinyxml2::XML_SUCCESS)
    {
        XMLElement* Root = XMLDoc_local.RootElement();
        if(Root)
        {
            // AxisParam_节点
            XMLElement* AxisParam_ = Root->FirstChildElement("AxisParam_");
            if(AxisParam_)
            {
                XMLElement* AxisY = AxisParam_->FirstChildElement("AxisY");
                if(AxisY)
                {
                    Pendant_Techn_Param_ptr->AxisY.AxisPos = QString(AxisY->Attribute("AxisY_Pos")).toDouble();
                    Pendant_Techn_Param_ptr->AxisY.EncodePos = QString(AxisY->Attribute("AxisY_EncodePos")).toInt();
                    std::istringstream(AxisY->Attribute("AxisY_IsZero")) >> std::boolalpha >> Pendant_Techn_Param_ptr->AxisY.IsZero;
                    Pendant_Techn_Param_ptr->AxisY.AxisAcc = QString(AxisY->Attribute("AxisY_AxisAcc")).toInt();
                    Pendant_Techn_Param_ptr->AxisY.AxisAccTime = QString(AxisY->Attribute("AxisY_AxisAccTime")).toFloat();
                }
                XMLElement* AxisMX = AxisParam_->FirstChildElement("AxisMX");
                if(AxisMX)
                {
                    Pendant_Techn_Param_ptr->AxisMX.AxisPos = QString(AxisMX->Attribute("AxisMX_Pos")).toDouble();
                    Pendant_Techn_Param_ptr->AxisMX.EncodePos = QString(AxisMX->Attribute("AxisMX_EncodePos")).toInt();
                    std::istringstream(AxisMX->Attribute("AxisMX_IsZero")) >> std::boolalpha >> Pendant_Techn_Param_ptr->AxisMX.IsZero;
                    Pendant_Techn_Param_ptr->AxisMX.AxisAcc = QString(AxisMX->Attribute("AxisMX_AxisAcc")).toInt();
                    Pendant_Techn_Param_ptr->AxisMX.AxisAccTime = QString(AxisMX->Attribute("AxisMX_AxisAccTime")).toFloat();
                }
                XMLElement* AxisMZ = AxisParam_->FirstChildElement("AxisMZ");
                if(AxisMZ)
                {
                    Pendant_Techn_Param_ptr->AxisMZ.AxisPos = QString(AxisMZ->Attribute("AxisMZ_Pos")).toDouble();
                    Pendant_Techn_Param_ptr->AxisMZ.EncodePos = QString(AxisMZ->Attribute("AxisMZ_EncodePos")).toInt();
                    std::istringstream(AxisMZ->Attribute("AxisMZ_IsZero")) >> std::boolalpha >> Pendant_Techn_Param_ptr->AxisMZ.IsZero;
                    Pendant_Techn_Param_ptr->AxisMZ.AxisAcc = QString(AxisMZ->Attribute("AxisMZ_AxisAcc")).toInt();
                    Pendant_Techn_Param_ptr->AxisMZ.AxisAccTime = QString(AxisMZ->Attribute("AxisMZ_AxisAccTime")).toFloat();
                }
                XMLElement* AxisSX = AxisParam_->FirstChildElement("AxisSX");
                if(AxisSX)
                {
                    Pendant_Techn_Param_ptr->AxisSX.AxisPos = QString(AxisSX->Attribute("AxisSX_Pos")).toDouble();
                    Pendant_Techn_Param_ptr->AxisSX.EncodePos = QString(AxisSX->Attribute("AxisSX_EncodePos")).toInt();
                    std::istringstream(AxisSX->Attribute("AxisSX_IsZero")) >> std::boolalpha >> Pendant_Techn_Param_ptr->AxisSX.IsZero;
                    Pendant_Techn_Param_ptr->AxisSX.AxisAcc = QString(AxisSX->Attribute("AxisSX_AxisAcc")).toInt();
                    Pendant_Techn_Param_ptr->AxisSX.AxisAccTime = QString(AxisSX->Attribute("AxisSX_AxisAccTime")).toFloat();
                }
                XMLElement* AxisSZ = AxisParam_->FirstChildElement("AxisSZ");
                if(AxisSZ)
                {
                    Pendant_Techn_Param_ptr->AxisSZ.AxisPos = QString(AxisSZ->Attribute("AxisSZ_Pos")).toDouble();
                    Pendant_Techn_Param_ptr->AxisSZ.EncodePos = QString(AxisSZ->Attribute("AxisSZ_EncodePos")).toInt();
                    std::istringstream(AxisSZ->Attribute("AxisSZ_IsZero")) >> std::boolalpha >> Pendant_Techn_Param_ptr->AxisSZ.IsZero;
                    Pendant_Techn_Param_ptr->AxisSZ.AxisAcc = QString(AxisSZ->Attribute("AxisSZ_AxisAcc")).toInt();
                    Pendant_Techn_Param_ptr->AxisSZ.AxisAccTime = QString(AxisSZ->Attribute("AxisSZ_AxisAccTime")).toFloat();
                }
            }
            // IoParam_节点
            XMLElement* IoParam_ = Root->FirstChildElement("IoParam_");
            if(IoParam_)
            {
                XMLElement* X04 = IoParam_->FirstChildElement("X04");
                if(X04)
                    Pendant_Techn_Param_ptr->PendantIoParam.X04 = QString(X04->Attribute("IoState")).toInt();
                XMLElement* Y01 = IoParam_->FirstChildElement("Y01");
                if(Y01)
                    Pendant_Techn_Param_ptr->PendantIoParam.Y01 = QString(Y01->Attribute("IoState")).toInt();
                XMLElement* EmbraceOpen1 = IoParam_->FirstChildElement("EmbraceOpen1");
                if(EmbraceOpen1)
                    Pendant_Techn_Param_ptr->PendantIoParam.EmbraceOpen1 = QString(EmbraceOpen1->Attribute("IoState")).toInt();
            }
            // ServoParam_节点
            XMLElement* ServoParam_ = Root->FirstChildElement("ServoParam_");
            if(ServoParam_)
            {
                XMLElement* SpeedRatio = ServoParam_->FirstChildElement("SpeedRatio");
                if(SpeedRatio)
                    Pendant_Techn_Param_ptr->PendantServoParam.SpeedRatio = QString(SpeedRatio->Attribute("ServoValue")).toInt();
                XMLElement* CorrectiveAxisAcc = ServoParam_->FirstChildElement("CorrectiveAxisAcc");
                if(CorrectiveAxisAcc)
                    Pendant_Techn_Param_ptr->PendantServoParam.CorrectiveAxisAcc = QString(CorrectiveAxisAcc->Attribute("ServoValue")).toInt();
            }
        }
    }
}
